package bookstore;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 書籍データを表すBeanです
 */
public class Book implements java.io.Serializable {
    /**
	 *
	 */
	private static final long serialVersionUID = 1L;
	/** 書籍番号 */
    private String id;
    /** 書籍名 */
    private String title;
    /** 著者名 */
    private String author;
    /** 出版社名 */
    private String publisher;
    /** 価格 */
    private int price;
    /** SQL文	**/
    private static String sql = "SELECT id, title, author, publisher, price FROM Book";

    // DBから登録済みのデータを取得
    public static Map<String,Book> loadAllData() throws SQLException {
        Map<String, Book> bookMap = new HashMap<String, Book>();
        Connection conn = null;
        try {
            conn = new ResourceFinder().getConnection();
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                Book book = new Book();
                book.setId(rs.getString(1));
                book.setTitle(rs.getString(2));
                book.setAuthor(rs.getString(3));
                book.setPublisher(rs.getString(4));
                book.setPrice(rs.getInt(5));
                bookMap.put(book.getId(), book);
                System.out.println(book.getTitle());
            }
            rs.close();
            st.close();

        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ignore) {}
            }
        }
        return bookMap;
    }

    // 書籍マスタデータを対象に、キーワード検索を行う
    public static Book[] searchByKeyword(Book[] books, String keyword) {
        List<Book> bookList = new ArrayList<Book>();
        for (int i = 0; i < books.length; i++) {
            if (books[i].getTitle().indexOf(keyword) > -1) {
                bookList.add(books[i]);
            } else if (books[i].getAuthor().indexOf(keyword) > -1) {
                bookList.add(books[i]);
            } else if (books[i].getPublisher().indexOf(keyword) > -1) {
                bookList.add(books[i]);
            }
        }
        return (Book[]) bookList.toArray(new Book[bookList.size()]);
    }

    // 指定した書籍番号を持つBook配列を取得
    public static Book[] searchByIds(Book[] books, String[] ids) {
        List<Book> list = new ArrayList<Book>();
        for (int i = 0; i < books.length; i++) {
            for (int j = 0; j < ids.length; j++) {
                if (books[i].getId().equals(ids[j])) {
                    list.add(books[i]);
                    break;
                }
            }
        }
        return (Book[]) list.toArray(new Book[list.size()]);
    }

    // getter/setterメソッド
    public String getAuthor() {
        return author;
    }

    public String getId() {
        return id;
    }

    public String getPublisher() {
        return publisher;
    }

    public String getTitle() {
        return title;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}